﻿Namespace Packets.UDP

    Module PacketsUDP

        Public Sub PacketIn(ByVal info As Packet, ByVal struct() As Byte)

            'check if this server is in the cluster, without this errors occur if debugging & running a server on the lan
            If Not Socket.HTTPStats.Data.ClusterStats.Hosts.ContainsKey(info.Username) Then
                Exit Sub
            End If

            Select Case CType(info.PacketType, InterServerPacketType)

                Case InterServerPacketType.RefreshStat
                    Select Case info.PacketOpCode
                        Case PacketOpCode.Notify
                            Dim statinfo As RefreshStat = ArrayToStruct(struct, GetType(RefreshStat))
                            Socket.HTTPStats.Data.ClusterStats.RemoteStatUpdate(statinfo)
                    End Select

                Case InterServerPacketType.XServerChatMessage
                    Select Case info.PacketOpCode
                        Case PacketOpCode.Notify

                            Dim chatmsg As CharChatMsg = ArrayToStruct(struct, GetType(CharChatMsg))

                            Select Case chatmsg.BYTE_TYPE

                                Case ChatMsgType.GlobalShout, ChatMsgType.Admin, ChatMsgType.Server

                                    Dim chatpkt As New Packet
                                    chatpkt.PacketType = GamePacketType.ChatMsg
                                    chatpkt.PacketOpCode = PacketOpCode.Notify
                                    SendToAllInGame(chatpkt, chatmsg)

                                Case ChatMsgType.Whisper

                                    WhisperChat(chatmsg)

                            End Select

                    End Select

                Case InterServerPacketType.UserLogin
                    Select Case info.PacketOpCode
                        Case PacketOpCode.Notify
                            Dim pinfo As PlayerOnlineInfo = ArrayToStruct(struct, GetType(PlayerOnlineInfo))

                            Socket.HTTPStats.Data.OnlineUsers.Online(pinfo)

                    End Select

                Case InterServerPacketType.UserLogout
                    Select Case info.PacketOpCode
                        Case PacketOpCode.Notify
                            Dim pinfo As PlayerOnlineInfo = ArrayToStruct(struct, GetType(PlayerOnlineInfo))

                            Socket.HTTPStats.Data.OnlineUsers.Offline(pinfo)

                    End Select

                Case InterServerPacketType.ServerOnline
                    Select Case info.PacketOpCode
                        Case PacketOpCode.Notify

                            Dim host As ServerHost = ArrayToStruct(struct, GetType(ServerHost))
                            If host.STR_HOSTNAME <> Globals.ServerName Then
                                Socket.HTTPStats.Data.OnlineUsers.RefreshNewServer()
                            End If

                    End Select


            End Select

        End Sub

        Private Sub WhisperChat(M As CharChatMsg)

            Dim ssql As String = "SELECT ID_CHARACTER FROM TBL_CHARACTER WHERE STR_NAME='" & M.STR_TONAME & "';"
            Dim row As Collection = DB.GameDB.ExecuteQueryFirstRecord(ssql)

            If Not row Is Nothing Then
                If row.Count > 0 Then

                    Dim conn As Connection = Socket.SockServer.ConnectionList.GetConnFromCharacterID(row("ID_CHARACTER"))

                    If Not conn Is Nothing Then

                        Dim pkt As Packet

                        pkt.PacketType = GamePacketType.ChatMsg
                        pkt.PacketOpCode = PacketOpCode.Notify
                        pkt.Username = conn.Username

                        Socket.SockServer.SendData(Protocol.MakePacket(pkt, M, conn.Crypto), conn.Index)

                    End If

                End If
            End If


        End Sub

    End Module

End Namespace